//
//  UICollectionViewCell+Extension.swift
//  huangshi
//
//  Created by shuguan wang on 2018/1/29.
//  Copyright © 2018年 shuguan wang. All rights reserved.
//

import UIKit

extension UICollectionReusableView {
    static func nib() -> UINib {
        return UINib(nibName: identifier, bundle: nil)
    }
    static var identifier:String{
        get{
            return NSStringFromClass(classForCoder()).components(separatedBy: ".").last!
        }
    }
}
class LeftEqualFlowLayout: UICollectionViewFlowLayout {
    static let leftMargin:CGFloat = 10
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        guard let attrs = super.layoutAttributesForElements(in: rect) else {
            return nil
        }
        for i in 0..<attrs.count {
            if i > 0 {
                let preFrame = attrs[i - 1].frame
                let attr = attrs[i]
                var frame = attr.frame
                if preFrame.origin.y == frame.origin.y {
                    frame.origin.x = preFrame.maxX + self.minimumInteritemSpacing
                    attr.frame = frame
                }else{
                    frame.origin.x = LeftEqualFlowLayout.leftMargin
                    attr.frame = frame
                }
            }else{
                let attr = attrs[i]
                var frame = attr.frame
                frame.origin.x = LeftEqualFlowLayout.leftMargin
                attr.frame = frame
            }
        }
        return attrs
    }
}
